﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using GzipManagerPro.IO;
using GzipManagerPro.UI.Dialogs;

namespace GzipManagerPro.UI.Utility
{
    public static class Helper
    {
        private static ProgressDialog Progress;
        #region Extract
        public static bool ExtractTo(string path)
        {
            return ExtractTo(path, null);
        }
        public static bool ExtractTo(string path, ArchiveItem item)
        {
            string password = null;
            return ExtractTo(path, item, ref password);
        }
        public static bool ExtractTo(string path, ArchiveItem item, ref string password)
        {
            return ExtractTo(Path.GetDirectoryName(path), path, item, ref password);
        }
        public static bool ExtractTo(string path, string archivePath, ArchiveItem item)
        {
            string password = null;
            return ExtractTo(path, archivePath, item, ref password);
        }
        public static bool ExtractTo(string path, string archivePath, ArchiveItem item, ref string password)
        {
            //string password = null;
            Archive archive = Archive.FromFile(archivePath);
            archive.ArchiveMessage += (s, e) => Progress.TranslateMessage(e);
            if (archive.IncludePassword && password == null)
            {
                PasswordDialog dialog = new PasswordDialog();

                if (dialog.ShowDialog() == DialogResult.Cancel)
                    return false;

                password = dialog.Value;
            }

            
            try
            {
                Progress = new ProgressDialog()
                {
                    Text = string.Format("Extracting {0}", System.IO.Path.GetFileName(archive.FileName))
                };

                Progress.Show();
                Application.DoEvents();
                if (item != null)
                    item.ExtractTo(path, archive, password);
                else
                    archive.ExtractTo(System.IO.Path.GetDirectoryName(path) ?? path, password);

                return true;
            }
            catch (Exception ex)
            {
                ex.MessageBox();
                return false;
            }
            finally
            {
                Application.DoEvents();
                Progress.Close();
                Application.DoEvents();
            }
        }
        #endregion
        #region Archive
        public static bool AddToArchive(string path)
        {
            return AddToArchive(path, true);
        }
        public static bool AddToArchive(string path, bool useFilePath)
        {
            return Utility.Helper.AddToArchive(new CustomFileSystemInfo(
                        Directory.Exists(path) ? (FileSystemInfo)new DirectoryInfo(path) : (FileSystemInfo)new FileInfo(path)
                        ), true);
        }
        public static bool AddToArchive(CustomFileSystemInfo info, bool useFilePath)
        {

            Archive archive = new Archive();
            archive.FileName = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(info.FullName),
                info.Name);

            archive.FileName += Archive.Extension;

            if (!useFilePath)
            {

                SaveFileDialog saveDialog = new SaveFileDialog()
                {
                    Filter = Archive.ExtensionFilter,
                    FileName = archive.FileName
                };

                if (saveDialog.ShowDialog() == DialogResult.Cancel)
                    return false;

                archive.FileName = saveDialog.FileName;
            }

            ArchiveDialog dialog = new ArchiveDialog();
            if (dialog.ShowDialog() == DialogResult.Cancel)
                return false;

            Progress = new ProgressDialog()
            {
                Text = "Creating archive..."
            };
            Progress.Show();
            Application.DoEvents();

            archive.StoryBoard = dialog.StoryBoardValue;
            archive.ArchiveMessage += (s, e) => Progress.TranslateMessage(e);

            if (!string.IsNullOrEmpty(dialog.PasswordValue))
                archive.SetPassword(dialog.PasswordValue);

            if (info.IsDirectory)
                archive.AddDirectory(info.FullName);
            else
                archive.AddFile(info.FullName);



            archive.SaveToFile();
            Application.DoEvents();
            Progress.Close();
            Application.DoEvents();

            return true;
        }
        #endregion
        #region Open
        public static bool Open(Archive archive, ArchiveItem item)
        {
            string path = Path.GetTempPath();
            string fileName = Path.Combine(path, item.Name);

            try
            {
                System.IO.File.Delete(fileName);
            }
            catch
            {
            }

            string password = null;
            ExtractTo(path, archive.FileName, item, ref password);

            try
            {
                if (!System.IO.File.Exists(fileName))
                    return false;

                System.Diagnostics.Process.Start(fileName);
                return false;
            }
            catch
            {
                return false;
            }
        }
        #endregion
    }
}
